package com.funo.logic.service;

import cn.hutool.json.JSONUtil;
import com.funo.logic.model.database.Table;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class TableService extends AbstractService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public Table getByModule(String moduleName) {
        Set<String> keys = stringRedisTemplate.keys(getCachePrefix() + "*");
        for (String key : keys) {
            String value = stringRedisTemplate.opsForValue().get(key);
            if (StringUtils.isBlank(value)) {
                continue;
            }
            Table table = JSONUtil.toBean(value, Table.class);
            if (moduleName.equals(table.getEntityName())) {
                return table;
            }
        }
        return null;
    }

    @Override
    public String getCachePrefix() {
        return "table:";
    }
}
